package com.jetbrains.licenseService.util;


import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;


class Entities {
    private static final String[][] a = {{"quot", "34"}, {"amp", "38"}, {"lt", "60"}, {"gt", "62"}};
    private static final String[][] b = {{"apos", "39"}};
    public static final Entities XML = new Entities();

    static {
        XML.addEntities(a);
        XML.addEntities(b);
    }

    static abstract interface EntityMap {
        public abstract void add(String paramString, int paramInt);

        public abstract String name(int paramInt);

        public abstract int value(String paramString);
    }

    static class PrimitiveEntityMap
            implements Entities.EntityMap {
        private Map a = new HashMap();
        private IntHashMap b = new IntHashMap();

        public void add(String paramString, int paramInt) {
            this.a.put(paramString, Integer.valueOf(paramInt));
            this.b.put(paramInt, paramString);
        }

        public String name(int paramInt) {
            return (String) this.b.get(paramInt);
        }

        public int value(String paramString) {
            Object localObject = this.a.get(paramString);
            if (localObject == null) {
                return -1;
            }
            return ((Integer) localObject).intValue();
        }
    }

    static abstract class MapIntMap
            implements Entities.EntityMap {
        protected Map mapNameToValue;
        protected Map mapValueToName;

        public void add(String paramString, int paramInt) {
            this.mapNameToValue.put(paramString, Integer.valueOf(paramInt));
            this.mapValueToName.put(Integer.valueOf(paramInt), paramString);
        }

        public String name(int paramInt) {
            return (String) this.mapValueToName.get(Integer.valueOf(paramInt));
        }

        public int value(String paramString) {
            Object localObject = this.mapNameToValue.get(paramString);
            if (localObject == null) {
                return -1;
            }
            return ((Integer) localObject).intValue();
        }
    }

    static class HashEntityMap
            extends Entities.MapIntMap {
        public HashEntityMap() {
            this.mapNameToValue = new HashMap();
            this.mapValueToName = new HashMap();
        }
    }

    static class TreeEntityMap
            extends Entities.MapIntMap {
        public TreeEntityMap() {
            this.mapNameToValue = new TreeMap();
            this.mapValueToName = new TreeMap();
        }
    }

    static class LookupEntityMap
            extends Entities.PrimitiveEntityMap {
        private String[] c;
        private int d = 256;

        public String name(int paramInt) {
            if (paramInt < this.d) {
                return a()[paramInt];
            }
            return super.name(paramInt);
        }

        private String[] a() {
            if (this.c == null) {
                b();
            }
            return this.c;
        }

        private void b() {
            this.c = new String[this.d];
            for (int i = 0; i < this.d; i++) {
                this.c[i] = super.name(i);
            }
        }
    }

    static class ArrayEntityMap
            implements Entities.EntityMap {
        protected int growBy = 100;
        protected int size = 0;
        protected String[] names;
        protected int[] values;

        public ArrayEntityMap() {
            this.names = new String[this.growBy];
            this.values = new int[this.growBy];
        }

        public ArrayEntityMap(int paramInt) {
            this.growBy = paramInt;
            this.names = new String[paramInt];
            this.values = new int[paramInt];
        }

        public void add(String paramString, int paramInt) {
            ensureCapacity(this.size + 1);
            this.names[this.size] = paramString;
            this.values[this.size] = paramInt;
            this.size += 1;
        }

        protected void ensureCapacity(int paramInt) {
            if (paramInt > this.names.length) {
                int i = Math.max(paramInt, this.size + this.growBy);
                String[] arrayOfString = new String[i];
                System.arraycopy(this.names, 0, arrayOfString, 0, this.size);
                this.names = arrayOfString;
                int[] arrayOfInt = new int[i];
                System.arraycopy(this.values, 0, arrayOfInt, 0, this.size);
                this.values = arrayOfInt;
            }
        }

        public String name(int paramInt) {
            for (int i = 0; i < this.size; i++) {
                if (this.values[i] == paramInt) {
                    return this.names[i];
                }
            }
            return null;
        }

        public int value(String paramString) {
            for (int i = 0; i < this.size; i++) {
                if (this.names[i].equals(paramString)) {
                    return this.values[i];
                }
            }
            return -1;
        }
    }

    static class BinaryEntityMap
            extends Entities.ArrayEntityMap {
        public BinaryEntityMap() {
        }

        public BinaryEntityMap(int paramInt) {
            super();
        }

        private int a(int paramInt) {
            int i = 0;
            int j = this.size - 1;
            while (i <= j) {
                int k = i + j >> 1;
                int m = this.values[k];
                if (m < paramInt) {
                    i = k + 1;
                } else if (m > paramInt) {
                    j = k - 1;
                } else {
                    return k;
                }
            }
            return -(i + 1);
        }

        public void add(String paramString, int paramInt) {
            ensureCapacity(this.size + 1);
            int i = a(paramInt);
            if (i > 0) {
                return;
            }
            i = -(i + 1);
            System.arraycopy(this.values, i, this.values, i + 1, this.size - i);
            this.values[i] = paramInt;
            System.arraycopy(this.names, i, this.names, i + 1, this.size - i);
            this.names[i] = paramString;
            this.size += 1;
        }

        public String name(int paramInt) {
            int i = a(paramInt);
            if (i < 0) {
                return null;
            }
            return this.names[i];
        }
    }

    EntityMap map = new LookupEntityMap();

    public void addEntities(String[][] paramArrayOfString) {
        for (String[] arrayOfString1 : paramArrayOfString) {
            addEntity(arrayOfString1[0], Integer.parseInt(arrayOfString1[1]));
        }
    }

    public void addEntity(String paramString, int paramInt) {
        this.map.add(paramString, paramInt);
    }

    public String entityName(int paramInt) {
        return this.map.name(paramInt);
    }

    public int entityValue(String paramString) {
        return this.map.value(paramString);
    }

    public String escape(String paramString) {
        StringWriter localStringWriter = a(paramString);
        try {
            escape(localStringWriter, paramString);
        } catch (IOException localIOException) {
        }
        return localStringWriter.toString();
    }

    public void escape(Writer paramWriter, String paramString)
            throws IOException {
        int i = paramString.length();
        for (int j = 0; j < i; j++) {
            int k = paramString.charAt(j);
            String str = entityName(k);
            if (str == null) {
                if (k > 127) {
                    paramWriter.write("&#");
                    paramWriter.write(Integer.toString(k, 10));
                    paramWriter.write(59);
                } else {
                    paramWriter.write(k);
                }
            } else {
                paramWriter.write(38);
                paramWriter.write(str);
                paramWriter.write(59);
            }
        }
    }

    public String unescape(String paramString) {
        int i = paramString.indexOf('&');
        if (i < 0) {
            return paramString;
        }
        StringWriter localStringWriter = a(paramString);
        try {
            a(localStringWriter, paramString, i);
        } catch (IOException localIOException) {
        }
        return localStringWriter.toString();
    }

    private StringWriter a(String paramString) {
        return new StringWriter((int) (paramString.length() + paramString.length() * 0.1D));
    }

    public void unescape(Writer paramWriter, String paramString)
            throws IOException {
        int i = paramString.indexOf('&');
        if (i < 0) {
            paramWriter.write(paramString);
        } else {
            a(paramWriter, paramString, i);
        }
    }

    private void a(Writer paramWriter, String paramString, int paramInt)
            throws IOException {
        paramWriter.write(paramString, 0, paramInt);
        int i = paramString.length();
        for (int j = paramInt; j < i; j++) {
            int k = paramString.charAt(j);
            if (k == 38) {
                int m = j + 1;
                int n = paramString.indexOf(';', m);
                if (n == -1) {
                    paramWriter.write(k);
                } else {
                    int i1 = paramString.indexOf('&', j + 1);
                    if ((i1 != -1) && (i1 < n)) {
                        paramWriter.write(k);
                    } else {
                        String str = paramString.substring(m, n);
                        int i2 = -1;
                        int i3 = str.length();
                        if (i3 > 0) {
                            if (str.charAt(0) == '#') {
                                if (i3 > 1) {
                                    int i4 = str.charAt(1);
                                    try {
                                        switch (i4) {
                                            case 88:
                                            case 120:
                                                i2 = Integer.parseInt(str.substring(2), 16);
                                                break;
                                            default:
                                                i2 = Integer.parseInt(str.substring(1), 10);
                                        }
                                        if (i2 > 65535) {
                                            i2 = -1;
                                        }
                                    } catch (NumberFormatException localNumberFormatException) {
                                        i2 = -1;
                                    }
                                }
                            } else {
                                i2 = entityValue(str);
                            }
                        }
                        if (i2 == -1) {
                            paramWriter.write(38);
                            paramWriter.write(str);
                            paramWriter.write(59);
                        } else {
                            paramWriter.write(i2);
                        }
                        j = n;
                    }
                }
            } else {
                paramWriter.write(k);
            }
        }
    }
}
